package com.lx.bizimpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.lx.biz.IResourcebiz;
import com.lx.domain.SharingResource;

@Transactional
@Service("iResourceImpl")
public class IResourceImpl implements IResourcebiz {
	@Autowired
	private SessionFactory sessionFactory;  

	public void setSessionFactory(SessionFactory sessionFactory) {  
	    this.sessionFactory = sessionFactory;  
	}  

	@Override
	public List<SharingResource> listSharingResource(String rows, String page,
			String sort, String order, String ResourceName, String date_from,
			String date_to) {
		String hql = "FROM SharingResource " ;
		String hql2 ="";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Integer first = Integer.parseInt(rows) * (Integer.parseInt(page)-1);
		
		if (ResourceName !=null && !ResourceName.equals("")) {
			hql2 += "resourceName like :name AND " ;
			
		}
		if (date_from !=null && !date_from.equals("")) {
			hql2 += "creatDate >= :date_from  AND ";
			
		}
		if (date_to !=null && !date_to.equals("")) {
			hql2 += "creatDate <= :date_to AND ";
			
		}
		if (!hql2.equals("")) {
			hql2 = "WHERE " + hql2.substring(0,hql2.length()-4);
		}
		
		hql += hql2 + " ORDER BY "+ sort +" " +order + "";
	  	Query query =sessionFactory.getCurrentSession().createQuery(hql);
		if (ResourceName !=null && !ResourceName.equals("")) {
			query.setParameter("name", "%"+ResourceName+"%") ;
		
		}
		if (date_to !=null && !date_to.equals("")) {
			try {
				Date dateto = dateFormat.parse(date_to);
				query .setDate("date_to",dateto) ;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		if (date_from !=null && !date_from.equals("")) {
			try {
				Date datefrom = dateFormat.parse(date_from);
				query.setDate("date_from",datefrom) ;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		  query.setFirstResult(first) ;
		  query.setMaxResults((Integer.parseInt(rows))) ;
		 /*  query.setParameter("pageSize", (Integer.parseInt(rows)));
		   query.setParameter("first", first);
		   */
		   
		
		return query.list();
		
	}

	@Override
	public int countResource(String rows, String page, String sort,
			String order, String ResourceName, String date_from, String date_to) {
		String hql = "SELECT count(*) FROM SharingResource " ;
		String hql2 ="";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if (ResourceName !=null && !ResourceName.equals("")) {
			hql2 += "resourceName like :name AND " ;
			
		}
		if (date_from !=null && !date_from.equals("")) {
			hql2 += "creatDate >= :date_from  AND ";
			
		}
		if (date_to !=null && !date_to.equals("")) {
			hql2 += "creatDate <= :date_to AND ";
			
		}
		if (!hql2.equals("")) {
			hql2 = "WHERE " + hql2.substring(0,hql2.length()-4);
		}
		
		hql += hql2 ;
	  	Query query =sessionFactory.getCurrentSession().createQuery(hql);
		if (ResourceName !=null && !ResourceName.equals("")) {
			query.setParameter("name", "%"+ResourceName+"%") ;
		
		}
		if (date_to !=null && !date_to.equals("")) {
			try {
				Date dateto = dateFormat.parse(date_to);
				query .setDate("date_to",dateto) ;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		if (date_from !=null && !date_from.equals("")) {
			try {
				Date datefrom = dateFormat.parse(date_from);
				query.setDate("date_from",datefrom) ;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		Number count = (Number)query.list().get(0);
 		return count.intValue();
		
	}

	@Override
	public int delResource(String ids) {
		//批量删除方法
		Session session= sessionFactory.getCurrentSession();
		String hql = "DELETE from SharingResource s WHERE ";
		String[] str =ids.split(",");
		for (int i = 0; i < str.length; i++) {
			 if (i==0) {
			//前端传过来的为string类型 先转型 此处用hql连接方式组合
			hql= hql +"id =" +Integer.parseInt(str[i]);
			}else {
			hql =hql +"or id =" +Integer.parseInt(str[i]);
				}
			}
			session.createQuery(hql).executeUpdate();

		
		return str.length;
	}

	@Override
	public String resourceAdd(SharingResource sharingResource
			) {
		    
			sharingResource.setCreatDate(new Date());
			sessionFactory.getCurrentSession().saveOrUpdate(sharingResource);
			return "success" ;
		
	}

	@Override
	public List<SharingResource> resourcelist(String rows, String page,
			String sort, String order, String ResourceName, String ResourceType) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SharingResource> resListType(String rows, String ResourceType) {
		String hql = "From SharingResource where resourceType = ? ORDER BY creatDate DESC " ;
		Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
		query.setString(0, ResourceType) ;
		query.setMaxResults((Integer.parseInt(rows))) ;
		return query.list();
	}

	@Override
	public int countAll(String resourceType) {
		String hql = null ;
		if(resourceType!=null&&resourceType!="") {
			hql = "SELECT count(*) FROM SharingResource  WHERE resourceType = ?" ;
			Query query =sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString(0, resourceType) ;
			Number count = (Number)query.list().get(0);
			return count.intValue() ;
		}else {
			hql = "SELECT count(*) FROM SharingResource " ;
			Query query =sessionFactory.getCurrentSession().createQuery(hql) ;
			Number count = (Number)query.list().get(0);
			return count.intValue() ;
		}
		 
		
	}

	@Override
	public SharingResource findById(String id) {
		String hqlString = "FROM SharingResource WHERE id = ?" ;
		Query query = sessionFactory.getCurrentSession().createQuery(hqlString) ;
		query.setInteger(0, Integer.parseInt(id)) ;
		return (SharingResource)query.uniqueResult();
	}
		
	@Override
	public List<SharingResource> findResourceByNameType(String ResourceName,
			String ResourceType,int pageIndex ,int pageSize) {
		String hql = "FROM SharingResource ";
		Integer first = pageSize * (pageIndex-1);
		if((ResourceName==""||ResourceName==null)&&(ResourceType==null||ResourceType=="")){
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setFirstResult(first) ;
			query.setMaxResults(pageSize) ;
			return query.list() ;
		}else if((ResourceName!=""||ResourceName!=null)&&(ResourceType==""||ResourceType==null)){
			hql += "WHERE resourceName like :resourceName" ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceName", "%"+ResourceName+"%") ;
			query.setFirstResult(first) ;
			query.setMaxResults(pageSize) ;
			return query.list() ;
		}else if ((ResourceName!=""||ResourceName!=null)&&(ResourceType!=""||ResourceType!=null)) {
			hql += "WHERE resourceType = :resourceType  and resourceName like :resourceName" ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceName", "%"+ResourceName+"%") ;
			query.setString("resourceType", ResourceType) ;
			query.setFirstResult(first) ;
			query.setMaxResults(pageSize) ;
			return query.list() ;
			
		}else {
			hql += "WHERE resourceType = :resourceType" ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceType", ResourceType) ;
			query.setFirstResult(first) ;
			query.setMaxResults(pageSize) ;
			
			return query.list() ;
		}
		
	}

	@Override
	public int countByNameType(String ResourceName, String ResourceType) {
		String hql = "SELECT count(*) FROM SharingResource ";
		
		if((ResourceName==""||ResourceName==null)&&(ResourceType==""||ResourceType==null)){
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			Number count = (Number)query.list().get(0);
			return count.intValue() ;
			
		}else if((ResourceName!=""||ResourceName!=null)&&(ResourceType==""||ResourceType==null)){
			hql += "WHERE resourceName like :resourceName" ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceName", "%"+ResourceName+"%") ;
			Number count = (Number)query.list().get(0);
			return count.intValue() ;
		}else if ((ResourceName!=""||ResourceName!=null)&&(ResourceType!=""||ResourceType!=null)) {
			hql += "WHERE resourceName like :resourceName AND resourceType = :resourceType " ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceName", "%"+ResourceName+"%") ;
			query.setString("resourceType", ResourceType) ;
			Number count = (Number)query.list().get(0);
			return count.intValue() ;
			
		}else {
			hql += "WHERE resourceType = :resourceType" ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setString("resourceType", ResourceType) ;
			Number count = (Number)query.list().get(0);
			
			return count.intValue() ;
		}
		
	}
}
